package cn.rc.rcsystem.service.impl;


import cn.rc.rcsystem.domain.CarRental;
import cn.rc.rcsystem.domain.CarRentals;
import cn.rc.rcsystem.domain.LeaseOrderCar;
import cn.rc.rcsystem.domain.User;
import cn.rc.rcsystem.exception.GlobalException;
import cn.rc.rcsystem.exception.GlobalExceptionCodeEnum;
import cn.rc.rcsystem.feign.CarRentalService;
import cn.rc.rcsystem.feign.OlderService;
import cn.rc.rcsystem.feign.UserService;

import cn.rc.rcsystem.mapper.CarRentalsMapper;
import cn.rc.rcsystem.result.JSONResult;
import cn.rc.rcsystem.service.ICarRentalsService;
import cn.rc.rcsystem.util.AssertUtil;
import cn.rc.rcsystem.util.StrUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-09
 */
@Service
public class CarRentalsServiceImpl extends ServiceImpl<CarRentalsMapper, CarRentals> implements ICarRentalsService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private CarRentalService    carRentalService;
    @Autowired
    private UserService userService;
    @Autowired
    private OlderService olderService;
    @Override
    public JSONResult rent(CarRentals carRentals) {
        Date date = new Date();
        // 判断参数是否为空
        AssertUtil.isNotNull(carRentals, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        // 查询车辆是否存在
        String carNumber = carRentals.getCarNumber();
        EntityWrapper<CarRentals> wrapper = new EntityWrapper<>();
        wrapper.eq("car_number",carNumber);
        CarRentals carRentals1 = selectOne(wrapper);
        AssertUtil.isNotNull(carRentals1, GlobalExceptionCodeEnum.NO_VEHICLE_ALREADY_EXISTS_ERROR);
        // 查看用户是否存在并判断是否有租车资格
        JSONResult jsonResult = userService.get(carRentals.getUserId());
        AssertUtil.isTrue(jsonResult.isSuccess(),GlobalExceptionCodeEnum.ERROR);
        User user = JSONObject.parseObject(JSON.toJSONString(jsonResult.getData()), User.class);
        // 判断用户年龄是否达到租车要求
        Integer age = user.getAge();
        if(age<20){
            throw  new GlobalException(GlobalExceptionCodeEnum.UNDER_AGE_ERROR);
        }
        // 查询店铺的租车
/*        JSONResult charter = carRentalService.charter(carRentals.getShopId(), carRentals.getCarNumber());
        AssertUtil.isTrue(jsonResult.isSuccess(),GlobalExceptionCodeEnum.ERROR);
        CarRental carRental = JSONObject.parseObject(JSON.toJSONString(charter.getData()), CarRental.class);*/
        EntityWrapper<CarRentals> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("shop_id",carRentals.getShopId());
        wrapper1.eq("car_number",carRentals.getCarNumber());
        CarRentals carRental = selectOne(wrapper1);
        if(carRental.getState()==0){
            throw new GlobalException(GlobalExceptionCodeEnum.NO_VEHICLE_ALREADY_EXISTS_ERROR);
        }
        // 生成订单
        synchronized (this){
            EntityWrapper<CarRentals> wra = new EntityWrapper<>();
            wrapper.eq("car_number",carNumber);
            CarRentals carRentals2 = selectOne(wra);
            if(carRentals2.getState()==0){
                throw new GlobalException(GlobalExceptionCodeEnum.NO_VEHICLE_ALREADY_EXISTS_ERROR);
            }
            // 生成订单
            LeaseOrderCar leaseOrderCar = new LeaseOrderCar();
            leaseOrderCar.setState(1);
            leaseOrderCar.setSn(StrUtils.getComplexRandomString(16));
            leaseOrderCar.setCarRentalId(carRentals2.getId());
            leaseOrderCar.setUserId(1L);
            leaseOrderCar.setShopId(carRentals.getShopId());
            leaseOrderCar.setCreatetime(date);
            leaseOrderCar.setShopaddress("成都");
            leaseOrderCar.setDeposit(new BigDecimal(3000.00));
            leaseOrderCar.setBrand(carRentals.getBrand());
            olderService.addLeaseOrderCar(leaseOrderCar);
            // 将订单编号存放到redis并设置过期时间
            redisTemplate.opsForValue().set("leaseOlderCar"+leaseOrderCar.getSn(), leaseOrderCar,30, TimeUnit.MINUTES);
            carRental.setState(0);
            updateById(carRental);
        }
        return JSONResult.success();
    }

    @Override
    public JSONResult cargrounding(CarRentals carRental) {

        System.out.println(carRental);
        EntityWrapper<CarRentals> wrapper = new EntityWrapper<>();
        wrapper.eq("car_number",carRental.getCarNumber());
        CarRentals carRentals = selectOne(wrapper);
        if(carRentals==null){
            insert(carRental);
        }
        if(carRentals!=null){
            carRental.setId(carRentals.getId());
            updateById(carRental);
        }
        /*System.out.println(1/0);*/
        return JSONResult.success();
    }
}
